{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "import twitter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "api = twitter.Api(consumer_key='xxxxxxxxxxxxxxxxxxx',\n",
    "                  consumer_secret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',\n",
    "                  access_token_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',\n",
    "                  access_token_secret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "results_mag = api.GetSearch(\n",
    "    raw_query=\"l=pt&q=Lu OR do OR Magalu OR Magazine OR Luiza %40magazineluiza since%3A2019-11-08 until%3A2019-11-09&result_type=recent&tweet_mode=extended&count=100\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "sql_query = \"\"\n",
    "for result in results_mag:\n",
    "    data = result.__repr__().split(\"Created=\", 1)[1].split(\")\", 1)[0]\n",
    "    if not sql_query:\n",
    "        sql_query += \"    ('{:s}');\\n\".format(data.replace(\"'\", \"''\"))\n",
    "    else:\n",
    "        sql_query = \"    ('{:s}'),\\n\".format(data.replace(\"'\", \"''\")) + sql_query\n",
    "\n",
    "sql_query = \"INSERT INTO magazine_tweets(content)\\n\" \\\n",
    "            \"VALUES\\n\" + sql_query\n",
    "\n",
    "with open('sql_magazine_tweets_query.sql', \"w\", encoding=\"utf-8\") as f:\n",
    "    f.write(sql_query)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [
    "results_inter = api.GetSearch(\n",
    "    raw_query=\"q=Banco Inter \\\"banco inter\\\" lang%3Apt since%3A2019-11-08 until%3A2019-11-09&result_type=recent&tweet_mode=extended&count=100\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "sql_query = \"\"\n",
    "for result in results_inter:\n",
    "    data = result.__repr__().split(\"Created=\", 1)[1].split(\")\", 1)[0]\n",
    "    if not sql_query:\n",
    "        sql_query += \"    ('{:s}');\\n\".format(data.replace(\"'\", \"''\"))\n",
    "    else:\n",
    "        sql_query = \"    ('{:s}'),\\n\".format(data.replace(\"'\", \"''\")) + sql_query\n",
    "\n",
    "sql_query = \"INSERT INTO inter_tweets(content)\\n\" \\\n",
    "            \"VALUES\\n\" + sql_query\n",
    "\n",
    "with open('sql_inter_tweets_query.sql', \"w\", encoding=\"utf-8\") as f:\n",
    "    f.write(sql_query)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}